//
//  StuyCoureVideoViewController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/16.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "StuyCoureVideoViewController.h"
#import "ZJStudyCoureFooterView.h"
#import "SkinVideoViewController.h"
#import "ZJVideoControllerMeunView.h"
#import "ZJSCNoteModel.h"
#import "ZJSCSynopsisModel.h"
#import "ZJSCQusetionsSrectionModel.h"
#import "ZJStudyCoureSwithView.h"

#import "StudyCourseNoteViewController.h"
#import "StudyCourseQuestionsViewController.h"
#import "SendCommentView.h"
#import <IQKeyboardManager.h>
//笔记cell
#import "ZJSCNoteTableViewCell.h"
//简介cell
#import "ZJSCSynopsisTableViewCell.h"
//简介的SectionView
#import "ZJSCSynopsisSectionView.h"
//评论的cell
#import "ZJSCQusetionsTableViewCell.h"
//评论的sectionView
#import "ZJSCQusetionsSrectionView.h"
//没有数据的cell
#import "ZJSCUnDataTableViewCell.h"

#define NOTECELL @"ZJSCNoteTableViewCell"
#define SYNOPSISCELL @"ZJSCSynopsisTableViewCell"
#define SYNOPSISSCECTIONVIEW @"ZJSCSynopsisSectionView"
#define COMMENTCELL @"ZJSCQusetionsTableViewCell.h"
#define COMMENTSECTIONVIEW @"ZJSCQusetionsSrectionView"
#define UNDATACELL  @"ZJSCUnDataTableViewCell"
@interface StuyCoureVideoViewController ()<UITableViewDelegate,UITableViewDataSource,ZJStudyCoureFooterViewDelegate,ZJVideoControllerMeunViewDelegate,ZJStudyCoureSwithViewDelegate,ZJSCQusetionsTableViewCellDelegate,ZJSCQusetionsSrectionViewDelegate,ZJSCNoteTableViewCellDelegate,SendCommentViewDelegate>
{
    NSInteger _synopsis;/**< 课程简介 */
    NSInteger _comment;/**< 评论 */
    NSInteger _note;/**< 笔记 */
    UIView *_commentBGView;/** 评论页面背景View */
    BOOL _isCourseComment;/**< 判断是否是课程评论、还是评论-评论 */
    BOOL _isCommentORNote;/**< YES:评论 NO:笔记 */
    ZJSCQusetionsSrectionModel *_commentSectionViewModel;/**< 记录评论SectionView的model */
    
}
/** 视频播放控制器 */
@property (nonatomic, strong)  SkinVideoViewController *videoPlayer;
/** 视频下部控制器 */
@property (nonatomic, strong) ZJVideoControllerMeunView *videoPlayerMeun;
/** 下面的控制器 */
@property (nonatomic, strong) ZJStudyCoureFooterView *footerView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** tableview */
@property (nonatomic, strong) UITableView *tableView;
/** 选择VIew */
@property (nonatomic, strong) ZJStudyCoureSwithView *swithView ;
/** 评论模块 */
@property (nonatomic, strong) SendCommentView *sendCommentView;
@end

@implementation StuyCoureVideoViewController
- (void)viewDidDisappear:(BOOL)animated {
    if (!self.navigationController.viewControllers) {
        // 主动调用 cancel 方法销毁播放器
        [self.videoPlayer cancel];    // cancel方法中调用了cancelObserver
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    [super viewDidDisappear:animated];
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self networking_introduce];
    [self setupUI];
    self.navigationController.navigationBar.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentTimeAndTotalTime:) name:ZJVideoControllerMeunViewTime object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];/**< 键盘将要出现的通知 */
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDismiss:) name:UIKeyboardWillHideNotification object:nil];/**< 键盘将要消失的通知 */
    
}
- (void)setupUI{
    [self.view addSubview:self.footerView];
    [self.view addSubview:self.videoPlayerMeun];
    [self.view addSubview:self.swithView];
    [self.view addSubview:self.tableView];
    [self initVideoPlayer];
    _commentBGView = [[UIView alloc] initWithFrame:self.view.bounds];
    _commentBGView.backgroundColor = [UIColor blackColor];
    _commentBGView.alpha = 0.2;
    _commentBGView.hidden = YES;
    UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_tapGester)];
    [_commentBGView addGestureRecognizer:tapGester];
    [self.view addSubview:_commentBGView];
    [self.view addSubview:self.sendCommentView];
   
}


#pragma mark - 初始化播放器

- (void)initVideoPlayer{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.isPresented = NO;
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    // 播放指定 vid 的视频
    [self.videoPlayer setVid:@"873c41fa75775e5642add081643ffb75_8"];
    
    [self.view addSubview:self.videoPlayer.view];
    [self.videoPlayer setParentViewController:self];
    
    
    
    // 需要保留导航栏
    [self.videoPlayer keepNavigationBar:YES];
    [self.videoPlayer setNavigationController:self.navigationController];
    
    // 设置附加组件
    [self.videoPlayer setHeadTitle:self.video.title];
    [self.videoPlayer setEnableDanmuDisplay:NO];      // 不显示弹幕按钮
    [self.videoPlayer setEnableRateDisplay:NO];       // 不显示播放速率按钮
    
    UIImage *logo = [UIImage imageNamed:@"pvlogo.png"];
    [self.videoPlayer setLogo:logo location:PvLogoLocationTopLeft size:CGSizeMake(70, 30) alpha:0.8]; // 设置logo
    
    // 开启片头播放
    // self.videoPlayer.teaserEnable = YES;
    
    // 自动续播, 是否继续上次观看的位置
    self.videoPlayer.autoContinue = YES;
    
    // 开启弹幕
    [self.videoPlayer enableDanmu:NO];
    
    // 是否开启截图
    self.videoPlayer.enableSnapshot = NO;
    
    // 设置是否自动播放，默认为YES
    self.videoPlayer.shouldAutoplay = YES;
    
    
    /**
     *  ---- 回调代码块 ----
     */
    __weak typeof(self) weakSelf = self;
    [self.videoPlayer setPlayButtonClickBlock:^{
        weakSelf.videoPlayerMeun.startButton.selected = YES;
    }];
    [self.videoPlayer setPauseButtonClickBlock:^{
        weakSelf.videoPlayerMeun.startButton.selected = NO;
    }];
    
    [self.videoPlayer setFullscreenBlock:^{
        //NSLog(@"should hide toolbox in this viewcontroller if needed");
    }];
    [self.videoPlayer setShrinkscreenBlock:^{
        //NSLog(@"show toolbox back if needed");
    }];
    
    // 视频播放完成的回调代码块
    [self.videoPlayer setWatchCompletedBlock:^{
        NSLog(@"user watching completed");
    }];
    
}

#pragma mark - 数据源获取
//获取课程简介
- (void)networking_introduce{
    [self.dataArray removeAllObjects];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"COURSEWARE_ID":self.model.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL courseware] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//
            ZJSCSynopsisModel *model = [ZJSCSynopsisModel yy_modelWithJSON:object];
            NSArray *array = @[@[model]];
            _synopsis = weakSelf.dataArray.count;
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
            [weakSelf networking_comment];
        }
    } Fail:^(id object) {
        
    }];
    
}
/** 获取评论 */
- (void)networking_comment{
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"STUDENTID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"COURSEWAREID":self.model.ID,
                           @"PAGE_NUMBER":@"1",
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL listClassFAQ] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000" ]) {//获取成功
            NSMutableArray *array = [[NSArray yy_modelArrayWithClass:[ZJSCQusetionsSrectionModel class] json:object[@"CLASS_FAQ"]] mutableCopy];
            
            if (array.count > 0) {//有数据
                _comment = weakSelf.dataArray.count;
                if (array.count < 5) {//显示五条数据
                    [weakSelf.dataArray addObjectsFromArray:array];
                }else{
                    NSArray *newArray = [array subarrayWithRange:NSMakeRange(0, 5)];
                    [weakSelf.dataArray addObjectsFromArray:newArray];
                    [weakSelf.dataArray addObject:@[@{@"name":@"点击查看更多评论",@"type":@"1"}]];
                }
            }else{//没有数据
                [weakSelf.dataArray addObject:@[@{@"imageName":@"zanwupinglun",@"type":@"0"}]];
            }
            
            [weakSelf.tableView reloadData];
            [weakSelf networking_note];
        }
    } Fail:^(id object) {
        
    }];
    
}
/** 获取笔记 */
- (void)networking_note{
    
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"COURSEWARE_ID":self.model.ID,
                           @"PAGE_NUMBER":@"1",
                           @"SHOW_NUMBER":@"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCourseNote] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//
            NSMutableArray *array = [[NSArray yy_modelArrayWithClass:[ZJSCNoteModel class] json:object[@"NOTES"]] mutableCopy];
            if (array.count > 0) {
                _note = weakSelf.dataArray.count;
                if (array.count < 5) {//显示五条数据
                    [weakSelf.dataArray addObjectsFromArray:@[@[array]]];
                }else{
                    NSArray *newArray = [array subarrayWithRange:NSMakeRange(0, 5)];
                    [weakSelf.dataArray addObjectsFromArray:@[@[newArray]]];
                    [weakSelf.dataArray addObject:@[@{@"name":@"点击查看更多笔记",@"type":@"1"}]];
                }
            }else{
                [weakSelf.dataArray addObject:@[@{@"imageName":@"zanwubiji",@"type":@"0"}]];
            }
            [weakSelf.tableView reloadData];
        }
    } Fail:^(id object) {
        
    }];
}


#pragma mark - 通知
- (void)currentTimeAndTotalTime:(NSNotification *)info{
    
    self.videoPlayerMeun.currentTime.text = info.object[@"currentTime"];
    self.videoPlayerMeun.totalTime.text = info.object[@"totalTime"];
}

#pragma mark - 键盘处理

- (void)action_tapGester{
    [self.view endEditing:YES];

}

- (void)keyboardWillShow:(NSNotification *)notification {
    _commentBGView.hidden = NO;
    self.sendCommentView.hidden = NO;
    [self.view bringSubviewToFront:self.sendCommentView];
    
    CGFloat curkeyBoardHeight = [[[notification userInfo] objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
    
    NSTimeInterval interal = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //计算 视图应该移动的位置
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:interal animations:^{
    
        weakSelf.sendCommentView.frame = CGRectMake(0, [UIScreen zj_screenHeight] - weakSelf.sendCommentView.height - curkeyBoardHeight, [UIScreen zj_screenWith], weakSelf.sendCommentView.height);
    }];
}

- (void)keyboardWillDismiss:(NSNotification *)notification {
    _commentBGView.hidden = YES;
    self.sendCommentView.hidden = YES;
    NSTimeInterval interal = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //计算 视图应该移动的位置
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:interal animations:^{
        weakSelf.sendCommentView.frame = CGRectMake(0, [UIScreen zj_screenHeight], [UIScreen zj_screenWith], weakSelf.sendCommentView.height);
        
    }];
}



#pragma mark - ZJSCNoteTableViewCellDelegate（笔记cell） 协议方法
/** 删除笔记 */
- (void)ZJSCNoteTableViewCellDelegate_DeleteButton:(ZJSCNoteModel *)model{
    [self networking_introduce];
}

#pragma mark - ZJSCQusetionsSrectionViewDelegate（评论SectionView） 协议方法
//(点赞或评论)sectionView上的按钮点击并网络请求完成
- (void)ZJSCQusetionsSrectionViewDelegate_operateSuccessToModel:(ZJSCQusetionsSrectionModel *)model section:(NSInteger)section{
    
    self.dataArray[section] = model;
}
//点击评论
- (void)ZJSCQusetionsSrectionViewDelegate_CommentClickToModel:(ZJSCQusetionsSrectionModel *)model{
    _isCommentORNote = YES;
    _commentSectionViewModel = model;
    _isCourseComment = NO;
    [self.sendCommentView.commentTextView.textView becomeFirstResponder];
}
#pragma mark - ZJSCQusetionsTableViewCellDelegate （评论cell）协议方法
//Cell上的按钮被点击
- (void)ZJSCQusetionsTableViewCellDelegate_operateSuccessToModel:(ZJSCQusetionsCellModel *)model indexPatch:(NSIndexPath *)indexPatch{
    ZJSCQusetionsSrectionModel *sectionModel = self.dataArray[indexPatch.section];
    sectionModel.childModelArray[indexPatch.row] = model;
    
    self.dataArray[indexPatch.section] = sectionModel;
}
#pragma mark - ZJStudyCoureSwithViewDelegate（选择） 协议方法
- (void)ZJStudyCoureSwithViewDelegate_synopsisClick{
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)ZJStudyCoureSwithViewDelegate_commentClick{

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_comment] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)ZJStudyCoureSwithViewDelegate_noteClick{
    if (_note == 0) {
        [self.tableView setContentOffset:CGPointMake(0, self.tableView.contentSize.height - self.tableView.height) animated:YES];
    }else{
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_note] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
#pragma mark - ZJVideoControllerMeunViewDelegate(播放控制器) 协议方法

- (void)ZJVideoControllerMeunViewDelegate_startButton:(UIButton *)sender{
    if (sender.selected) {
        [self.videoPlayer playButtonClick];
    }else{
        [self.videoPlayer pauseButtonClick];
    }
}
- (void)ZJVideoControllerMeunViewDelegate_backward:(UIButton *)sender{
    self.videoPlayer.videoControl.slider.progressValue -= 30.0f;
    [self.videoPlayer setCurrentPlaybackTime:floor(self.videoPlayer.videoControl.slider.progressValue)];
}

- (void)ZJVideoControllerMeunViewDelegate_speed:(UIButton *)sender{
    self.videoPlayer.videoControl.slider.progressValue += 30.0f;
    [self.videoPlayer setCurrentPlaybackTime:floor(self.videoPlayer.videoControl.slider.progressValue)];
}


#pragma mark - ZJStudyCoureFooterViewDelegate（下部footerView） 协议方法
/** 评论输入框 */
- (void)ZJStudyCoureFooterViewDelegate_commentTextButtonClick{
    _isCommentORNote = YES;
    _isCourseComment = YES;
    self.sendCommentView.hidden = NO;
    [self.sendCommentView.commentTextView.textView becomeFirstResponder];
}
/** 评论按钮 */
- (void)ZJStudyCoureFooterViewDelegate_commentButtonClick{//跳评论页面
    __weak typeof(self) weakSelf = self;
    StudyCourseQuestionsViewController *vc = [StudyCourseQuestionsViewController new];
    vc.model = self.model;
    vc.currTime = self.videoPlayer.currentTime;
    vc.block = ^(){//刷新tableview
        [weakSelf networking_introduce];
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}
/** 笔记按钮 */
- (void)ZJStudyCoureFooterViewDelegate_noteButtonClick{
    _isCommentORNote = NO;
    self.sendCommentView.hidden = NO;
    [self.sendCommentView.commentTextView.textView becomeFirstResponder];
}
/** 收藏按钮 */
- (void)ZJStudyCoureFooterViewDelegate_collectButtonClick{

}

#pragma mark - SendCommentViewDelegate(发送评论) 协议方法
//发送评论
- (void)sendButtonClick{
    [self.view endEditing:YES];
    NSDictionary *dict = @{};
    if (_isCommentORNote) {//评论
        if (_isCourseComment) {//是课程评论
            dict = @{@"USERID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,//当前用户id
                     @"COURSEWAREID":self.model.ID,//课程id
                     @"ACCOUNTID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.ID,//账户id
                     @"CONTENT":self.sendCommentView.commentTextView.textView.text,//评论内容
                     @"VIDEOSTAMP":@(self.videoPlayer.currentTime),
                     @"UNITID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.CURRENTIDENTITY //机构id
                     };
        }else{//评论-评论
            dict = @{@"USERID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,//当前用户id
                     @"COURSEWAREID":self.model.ID,//课程id
                     @"ACCOUNTID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.ID,//账户id
                     @"CONTENT":self.sendCommentView.commentTextView.textView.text,//评论内容
                     @"VIDEOSTAMP":@(self.videoPlayer.currentTime),
                     @"UNITID":[ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.CURRENTIDENTITY, //机构id
                     @"FID":_commentSectionViewModel.ID
                     };
        }
        [self netWorkingCommentWithDict:dict];
        
    }else{//笔记
    
        [self netWorkingNote];
    }
}
//发送评论的网络请求
//评论
- (void)netWorkingCommentWithDict:(NSDictionary *)dict{
    [YJProgressHUD show:@"" inView:self.view mode:YJProgressModeLoading];
    __weak typeof(self) weakSelf = self;
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL addClassFAQ] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//评论成功
            [YJProgressHUD showMsgWithoutView:@"评论成功"];
            [weakSelf networking_introduce];
        }else{
            
            [YJProgressHUD showMsgWithoutView:@"评论失败"];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"评论失败"];
    }];
    
}
//写笔记的网络请求

//笔记
- (void)netWorkingNote{
    [YJProgressHUD show:@"" inView:self.view mode:YJProgressModeLoading];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USER_ID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,//用户id
                           @"VIDEO_STAMP":@(self.videoPlayer.currentTime),//视频时间
                           @"COURSEWARE_ID":self.model.ID,//课程id
                           @"UNITID":[ZJUserInfoModel sharedUserInfo].DEPT_MODEL_JOB.ID,//机构id
                           @"NOTE_CONTENT":self.sendCommentView.commentTextView.textView.text};//笔记内容
    [NetWorkingManager postWithUrlString:[NetWorkingURL courseNote] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        [YJProgressHUD hide];
        if ([model.STATE isEqualToString:@"20000"]) {//记笔记成功
            [weakSelf networking_introduce];
        }else{
            [YJProgressHUD showMsgWithoutView:@"记笔记失败"];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"记笔记失败"];
    }];
    
}

#pragma mark - UITableView 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    id model = self.dataArray[section];
    
    if ([model isKindOfClass:[NSArray class]]) {
        NSArray *array = model;
        if ([array.firstObject isKindOfClass:[ZJSCSynopsisModel class]]) { //简介
            return 1;
        }else if ([array.firstObject isKindOfClass:[NSArray class]]){ //笔记
            NSArray *noteArray = array.firstObject;
            if ([noteArray.firstObject isKindOfClass:[ZJSCNoteModel class]]) {
                return  noteArray.count;
            }
        }else if ([array.firstObject isKindOfClass:[NSDictionary class]]){//点击更多
            return 1;
        }
    }
    //评论
    if ([model isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        ZJSCQusetionsSrectionModel * newModel = model;
        return newModel.CHILD_CLASS_FAQ.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id model = self.dataArray[indexPath.section];
    if ([model isKindOfClass:[NSArray class]]) {
        NSArray *array = model;
        if ([array.firstObject isKindOfClass:[ZJSCSynopsisModel class]]) { //简介
            
            ZJSCSynopsisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SYNOPSISCELL forIndexPath:indexPath];
            
            if (indexPath.section == 1) {
                cell.isSynopsisIntroduce = YES;
                [self.swithView selectNumberString:@"1"];
            }else{
                cell.isSynopsisIntroduce = NO;
            }
            cell.model = array.firstObject;
            return cell;
            
        }else if ([array.firstObject isKindOfClass:[NSArray class]]){ //笔记
            NSArray *noteArray = array.firstObject;
            if ([noteArray.firstObject isKindOfClass:[ZJSCNoteModel class]]) { //笔记
                if (indexPath.row == 0) {
                    [self.swithView selectNumberString:@"3"];
                }
            ZJSCNoteModel *model = noteArray[indexPath.row];
            ZJSCNoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NOTECELL forIndexPath:indexPath];
            cell.model = model;
            cell.delegate = self;
            return cell;
        }
        }else if ([array.firstObject isKindOfClass:[NSDictionary class]]){//点击更多
            NSDictionary *dict = array.firstObject;
            if ([[dict objectForKey:@"type"] isEqualToString:@"1"]) {//点击更多
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.textLabel.font = [UIFont titleTextFont];
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.textLabel.textColor = [UIColor textBlackColor];
                cell.textLabel.text = dict[@"name"];
                cell.textLabel.backgroundColor = [UIColor clearColor];
                cell.contentView.backgroundColor = [UIColor viewBGGrayColor];
                return cell;

            }else{//显示无数据图片
                ZJSCUnDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UNDATACELL forIndexPath:indexPath];
                cell.unDataImageView.image = [UIImage imageNamed:[dict objectForKey:@"imageName"]];
                return cell;
            }
    }
}
    //评论
    if ([model isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        [self.swithView selectNumberString:@"2"];
        ZJSCQusetionsSrectionModel * sectionModel = model;
        ZJSCQusetionsCellModel *cellModel = sectionModel.childModelArray[indexPath.row];
        
        ZJSCQusetionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:COMMENTCELL forIndexPath:indexPath];
        cell.model = cellModel;
        cell.indexPatch = indexPath;
        cell.delegate = self;
        if (indexPath.row == sectionModel.CHILD_CLASS_FAQ.count - 1) {
            cell.isLastCell = YES;
        }else{
            cell.isLastCell = NO;
        }
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    id model = self.dataArray[section];
    if ([model isKindOfClass:[NSArray class]]) {
        NSArray *array = model;
        if ([array.firstObject isKindOfClass:[ZJSCSynopsisModel class]]) { //简介
            [self.swithView selectNumberString:@"1"];
            ZJSCSynopsisSectionView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SYNOPSISSCECTIONVIEW];
            if (section == 0) {
                view.titleLabel.text = @"主持人简介 :";
            }else{
                view.titleLabel.text = @"课程简介 :";
            }
            return view;
        }else if ([array.firstObject isKindOfClass:[NSArray class]]){ //笔记
            NSArray *noteArray = array.firstObject;
            if ([noteArray.firstObject isKindOfClass:[ZJSCNoteModel class]]) { //笔记
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
                [self.swithView selectNumberString:@"3"];
                return view;
            }
        }
    }
    //评论
    if ([model isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        [self.swithView selectNumberString:@"2"];
        ZJSCQusetionsSrectionModel * sectionModel = model;
        ZJSCQusetionsSrectionView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:COMMENTSECTIONVIEW];
        view.model = sectionModel;
        view.section = section;
        view.delegate = self;
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id model = self.dataArray[indexPath.section];
    if ([model isKindOfClass:[NSArray class]]) {
        NSArray *array = model;
        if ([array.firstObject isKindOfClass:[ZJSCSynopsisModel class]]) { //简介
            ZJSCSynopsisModel *synopsisModel = array.firstObject;
            if (indexPath.section == 1) {
                synopsisModel.isSynopsisIntroduce = YES;
            }else{
                synopsisModel.isSynopsisIntroduce = NO;
            }
            return synopsisModel.cellHeight;

        }else if ([array.firstObject isKindOfClass:[NSArray class]]){ //笔记
            NSArray *noteArray = array.firstObject;
            if ([noteArray.firstObject isKindOfClass:[ZJSCNoteModel class]]) { //笔记
                ZJSCNoteModel *model = noteArray[indexPath.row];
                return  model.cellHeight;
            }
        }else if ([array.firstObject isKindOfClass:[NSDictionary class]]){//点击更多
            NSDictionary *dict = array.firstObject;
            if ([[dict objectForKey:@"type"] isEqualToString:@"1"]) {//点击更多
                return 44 * [UIScreen zj_screenHScale];
            }else{//显示无数据图片
                return 88 * [UIScreen zj_screenHScale];
            }
        }
    }
            //评论
        if ([model isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
            ZJSCQusetionsSrectionModel * sectionModel = model;
            ZJSCQusetionsCellModel *cellModel = sectionModel.childModelArray[indexPath.row];
            
            if (indexPath.row == sectionModel.CHILD_CLASS_FAQ.count - 1) {
                cellModel.isLastCell = YES;
            }else{
                cellModel.isLastCell = NO;
            }
            return cellModel.cellHeight;
        }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    id model = self.dataArray[section];
    if ([model isKindOfClass:[NSArray class]]) {
        NSArray *array = model;
        if ([array.firstObject isKindOfClass:[ZJSCSynopsisModel class]]) { //简介

            return 44 * [UIScreen zj_screenHScale];
        }else if ([array.firstObject isKindOfClass:[NSArray class]]){ //笔记
            NSArray *noteArray = array.firstObject;
            if ([noteArray.firstObject isKindOfClass:[ZJSCNoteModel class]]) { //笔记
        
                return 0.1;
            }
        }else if ([array.firstObject isKindOfClass:[NSDictionary class]]){//点击更多
            return 0.1;
        }
    }
    //评论
    if ([model isKindOfClass:[ZJSCQusetionsSrectionModel class]]) {
        ZJSCQusetionsSrectionModel * newModel = model;
        return newModel.sectionHeight;
    }

    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.textLabel.text isEqualToString:@"点击查看更多评论"]) {
        __weak typeof(self) weakSelf = self;
        StudyCourseQuestionsViewController *vc = [StudyCourseQuestionsViewController new];
        vc.model = self.model;
        vc.currTime = self.videoPlayer.currentTime;
        vc.block = ^(){//刷新tableview
            [weakSelf networking_introduce];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"点击查看更多笔记"]) {
        __weak typeof(self) weakSelf = self;
        StudyCourseNoteViewController *vc = [StudyCourseNoteViewController new];
        vc.model = self.model;
        vc.currTime = self.videoPlayer.currentTime;
        vc.block = ^(){//刷新tableview
            [weakSelf networking_introduce];
        };

        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - Setter
#pragma mark - Getters

- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.swithView.bottom, self.view.width, self.footerView.y - self.swithView.bottom)  style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView  registerClass:[UITableViewCell class] forCellReuseIdentifier: @"Cell"];
        [_tableView  registerClass:[ZJSCNoteTableViewCell class] forCellReuseIdentifier: NOTECELL];
        [_tableView  registerClass:[ZJSCSynopsisTableViewCell class] forCellReuseIdentifier: SYNOPSISCELL];
        [_tableView  registerClass:[ZJSCQusetionsTableViewCell class] forCellReuseIdentifier: COMMENTCELL];
        [_tableView  registerClass:[ZJSCUnDataTableViewCell class] forCellReuseIdentifier: UNDATACELL];
      
        [_tableView registerClass:[ZJSCSynopsisSectionView class] forHeaderFooterViewReuseIdentifier:SYNOPSISSCECTIONVIEW];
        [_tableView registerClass:[ZJSCQusetionsSrectionView class] forHeaderFooterViewReuseIdentifier:COMMENTSECTIONVIEW];
     
    }
    return _tableView;
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (ZJStudyCoureFooterView *)footerView{
    if (_footerView == nil){
        _footerView = [[ZJStudyCoureFooterView alloc]initWithFrame:CGRectMake(0, self.view.height - 49, self.view.width, 49)];
        _footerView.delegate = self;
    }
    return _footerView;
}

- (SkinVideoViewController *)videoPlayer{
    if (!_videoPlayer) {
        CGFloat width = self.view.bounds.size.width;
        _videoPlayer = [[SkinVideoViewController alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 64, width, width*(9.0/16.0))];
        [_videoPlayer configObserver];
    }
    return _videoPlayer;
}

- (ZJVideoControllerMeunView *)videoPlayerMeun{
    if (_videoPlayerMeun == nil){
        CGFloat width = self.view.bounds.size.width;
        _videoPlayerMeun = [[ZJVideoControllerMeunView alloc] initWithFrame:CGRectMake(0, width*(9.0/16.0) + 64, self.view.width, 44)];
        _videoPlayerMeun.backgroundColor = [UIColor whiteColor];
        _videoPlayerMeun.delegate = self;
        _videoPlayerMeun.startButton.selected = YES;
        if (!self.isCandragging) {
            _videoPlayerMeun.backward.userInteractionEnabled = NO;
            _videoPlayerMeun.speed.userInteractionEnabled = NO;
        }
    }
    return _videoPlayerMeun;
}
- (ZJStudyCoureSwithView *)swithView{
    if (_swithView == nil){
        _swithView = [[ZJStudyCoureSwithView alloc] initWithFrame:CGRectMake(0, self.videoPlayerMeun.bottom, self.view.width, 44 * [UIScreen zj_screenHScale])];
        _swithView.delegate = self;
    }
    return _swithView;
}

- (SendCommentView *)sendCommentView{
    if (_sendCommentView == nil){
        _sendCommentView = [[SendCommentView alloc]initWithFrame:CGRectMake(0, [UIScreen zj_screenHeight], [UIScreen zj_screenWith], [UIScreen zj_screenHeight] * 0.2)];
        _sendCommentView.commentDelegate = self;
    }
    return _sendCommentView;
}

@end
